﻿using System;
using Fee.Stores;
using Fee.VendorMatchers;
using Identity.Stores;
using Identity.ViewModels;

namespace Fee.ViewModel
{
    public class VendorMatcherViewModel
    {
        public VendorMatcherViewModel()
        {
        }

        public VendorMatcherViewModel(VendorMatcher matcher)
        {
            Id = matcher.Id;
            Year = matcher.Year;
            ConstructionType = matcher.ConstructionType;
            if (matcher.District != null)
                District = new DistrictSimpleViewModel(matcher.District);
            Vendor = new VendorSimpleViewModel(matcher.Vendor);
            ContractType = matcher.ContractType;
            Year = matcher.Year;
            var constructionMatcher = matcher as ConstructionVendorMatcher;
            if (constructionMatcher != null)
            {
                IsVip = constructionMatcher.IsVip;
                StationType = constructionMatcher.StationType;
            }

        }

        public bool IsVip { get; set; }
        public ContractType ContractType { get; set; }
        public int Id { get; set; }
        public int Year { get; set; }
        public DistrictSimpleViewModel District { get; set; }

        /// <summary>
        /// </summary>
        public ConstructionType ConstructionType { get; set; }

        /// <summary>
        /// </summary>
        public VendorSimpleViewModel Vendor { get; set; }


        /// <summary>
        /// </summary>
        public StationType? StationType { get; set; }

        public VendorMatcher ToVendorMatcher<T>(IVendorStore<T> vendorStore, IDistrictStore<T> districtStore,IVendorMatcherStore<T> vendorMatcherStore)
            where T : FeeDbContext
        {
            VendorMatcher matcher;
            switch (ContractType)
            {
                case ContractType.Design:
                    matcher = this.Id != 0 ? vendorMatcherStore.GetById<DesignVendorMatcher>(this.Id):new DesignVendorMatcher();
                    break;
                case ContractType.Construction:
                    matcher =this.Id!=0? vendorMatcherStore.GetById<ConstructionVendorMatcher>(this.Id): new ConstructionVendorMatcher
                    {
                        StationType = StationType.Value,
                        ConstructionType = ConstructionType,
                        IsVip = IsVip
                    };
                    break
                        ;
                case ContractType.Supervisor:

                    matcher = this.Id != 0 ? vendorMatcherStore.GetById<SupervisorVendorMatcher>(this.Id) : new SupervisorVendorMatcher();
                    break;
                default:
                    throw new Exception();
            }

            matcher.Vendor = Vendor != null ? vendorStore.GetById(Vendor.Id) : null;
            matcher.District = District != null ? districtStore.GetById(District.Id) : null;
            matcher.ConstructionType = ConstructionType;
            matcher.Year = Year;
            return matcher;
        }
    }
}